<?php
    // +----------------------------------------------------------------------
    // | 所有Event控制器共有类
    // +----------------------------------------------------------------------
    // | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
    // +----------------------------------------------------------------------
    // | Author: 微尘 <yicmf@qq.com>
    // +----------------------------------------------------------------------
    namespace app\common\event;

    use think\Container;
    use think\Exception;
	use think\exception\ValidateException;
	use think\facade\App;
    use think\facade\Lang;
    use think\Model;
    use app\model\ucenter\User as UserModel;
	use think\Validate;

	class Base
    {
        /**
         * Request实例
         * @var \think\Request
         */
        protected $request;

		/**
		 * 是否批量验证
		 * @var bool
		 */
		protected $batchValidate = false;

        /**
         * 对应的模型
         * @var Model
         */
        protected $model;
        /**
         * 应用实例
         * @var \think\App
         */
        protected $app = null;

        public function __construct()
        {
            $this->app = app();
            $this->request = app('request');
            $this->model = str_replace('event', 'model', static::class);
            if (method_exists($this, 'initialize')) {
                $this->initialize();
            }
        }

		/**
		 * 验证数据
		 * @access protected
		 * @param  array        $data     数据
		 * @param  string|array $validate 验证器名或者验证规则数组
		 * @param  array        $message  提示信息
		 * @param  bool         $batch    是否批量验证
		 * @return array|string|true
		 * @throws ValidateException
		 */
		protected function validate(array $data, $validate, array $message = [], bool $batch = false)
		{
			if (is_array($validate)) {
				$v = new Validate();
				$v->rule($validate);
			} else {
				if (strpos($validate, '.')) {
					// 支持场景
					[$validate, $scene] = explode('.', $validate);
				}
				$class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
				$v     = new $class();
				if (!empty($scene)) {
					$v->scene($scene);
				}
			}

			$v->message($message);

			// 是否批量验证
			if ($batch || $this->batchValidate) {
				$v->batch(true);
			}
			return $v->failException(true)->check($data);
		}

        /**
         * 禁用后恢复
         * @param   array   $param
         * @param UserModel $user
         * @return mixed
         * @author 微尘 <yicmf@qq.com>
         * @datetime: 2020/6/30 11:35
         */
        public function restore($param, $user = null)
        {
            try {
                $result = $this->model::where('id', 'in', explode(',', $param['id']))->update(['status'=>1]);
                if (0 == $result) {
                    throw new Exception('恢复失败');
                }
                // 第1步 检测禁止发布的情况
                $data['code'] = 0;
                $data['message'] = '恢复成功';
                $data['data'] = $result;
            } catch (\Exception $e) {
                $data['code'] = 1;
                $data['message'] = $e->getMessage();
            }
            return $data;
        }
        /**
         * 禁用数据
         * @param   array   $param
         * @param UserModel $user
         * @return mixed
         * @author 微尘 <yicmf@qq.com>
         * @datetime: 2020/6/30 11:35
         */
        public function forbid($param, $user = null)
        {
            try {
                $result = $this->model::where('id', 'in', explode(',', $param['id']))->update(['status'=>-1]);
                if (0 == $result) {
                    throw new Exception('禁用失败');
                }
                // 第1步 检测禁止发布的情况
                $data['code'] = 0;
                $data['message'] = '禁用成功';
                $data['data'] = $result;
            } catch (\Exception $e) {
                $data['code'] = 1;
                $data['message'] = $e->getMessage();
            }
            return $data;
        }
        /**
         * 通用更新方法
         * @param array   $param
         * @param UserModel $user
         * @return mixed
         * @author 微尘 <yicmf@qq.com>
         * @datetime: 2020/6/30 17:31
         */
        public function update($param, $user = null)
        {
            try {
                if (empty($param)) {
                    throw new Exception(Lang::get('__data_format_error_'));
                }
                if (!empty($param['id'])) {
                    // 更新操作
                    $result = $this->model::update($param, ['id'=>$param['id']]);
                } else { // 新增
                    $result = $this->model::create($param);
                }
                $data['code'] = 0;
                $data['data'] = $result;
                $data['message'] = '操作成功';
            } catch (\Exception $e) {
                $data['code'] = 1;
                $data['message'] = $e->getMessage();
            }
            return $data;
        }


        /**
         * 删除数据
         * @param   array   $param
         * @param UserModel $user
         * @return mixed
         * @author 微尘 <yicmf@qq.com>
         * @datetime: 2020/6/30 11:35
         */
        public function del($param, $user = null)
        {
            try {
                $result = $this->model::where('id', 'in', explode(',', $param['id']))->update(['status'=>-2]);
                if (0 == $result) {
                    throw new Exception('删除失败');
                }
                // 第1步 检测禁止发布的情况
                $data['code'] = 0;
                $data['message'] = '删除成功';
                $data['data'] = $result;
            } catch (\Exception $e) {
                $data['code'] = 1;
                $data['message'] = $e->getMessage();
            }
            return $data;
        }
        /**
         * 删除数据
         * @param   array   $param
         * @param UserModel $user
         * @return mixed
         * @author 微尘 <yicmf@qq.com>
         */
        public function delete($param, $user = null)
        {
            try {
                $result = $this->model::where('id', 'in', explode(',', $param['id']))->update(['status'=>-2]);
                if (0 == $result) {
                    throw new Exception('删除失败');
                }
                // 第1步 检测禁止发布的情况
                $data['code'] = 0;
                $data['message'] = '删除成功';
                $data['data'] = $result;
            } catch (\Exception $e) {
                $data['code'] = 1;
                $data['message'] = $e->getMessage();
            }
            return $data;
        }

    }
